package com.bookyourtrek.model;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bookyourtrek.util.DbConnection;
import com.bookyourtrek.util.EmailManager;
import com.bookyourtrek.util.RuntimeSettings;

public class LoginModel {

	private PreparedStatement ps = null;
	private ResultSet rs = null;
	private String sqlQuery = "";
	private Connection conn = null;

	/**
	 * @param email
	 * @return true if email exist in the database or return false
	 * @throws SQLException
	 */
	public boolean checkEmail(String email) throws SQLException {
		sqlQuery = "SELECT * FROM bookyourtrek.user WHERE " + "email = ?";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setString(1, email);
			rs = ps.executeQuery();
			if (rs.next())
				return true;
			else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			
		}
		return false;
	}

	/**
	 * @param email
	 * @return user status active/inactive/banned
	 * @throws SQLException
	 */
	public Integer getUserStatus(String email) throws SQLException {
		sqlQuery = "SELECT status FROM bookyourtrek.user"
				+ " WHERE email = ?;";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setString(1, email);
			rs = ps.executeQuery();
			if (rs.next())
				return rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			
		}
		return null;
	}

	/**
	 * @param email
	 * @return password
	 * @throws SQLException
	 */
	public String getPassword(String email) throws SQLException {
		sqlQuery = "SELECT password FROM bookyourtrek.user"
				+ " WHERE email = ?;";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setString(1, email);
			rs = ps.executeQuery();
			if (rs.next())
				return rs.getString(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			
		}
		return null;
	}

	public BigInteger gettotalusers() {
		BigInteger totaluser = null;
		sqlQuery = "SELECT COUNT(userid) FROM bookyourtrek.user";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			rs = ps.executeQuery();
			while (rs.next()) {
				totaluser = BigInteger.valueOf(rs.getInt(1));
			}		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			
		}
		return totaluser;
	}

	public void updatePassword(String forgotemail, String resetpass) throws SQLException {
		sqlQuery = "UPDATE bookyourtrek.user SET password = ? WHERE email = ?;";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setString(1, resetpass);
			ps.setString(2, forgotemail);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	public String getPhone(String email) throws SQLException {
		String phoneno = null;
		sqlQuery = "SELECT phone FROM bookyourtrek.user WHERE email = ?";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setString(1, email);
			rs = ps.executeQuery();
			while (rs.next()) {
				phoneno = (rs.getString(1));
			}		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			
		}
		return phoneno;
	}

	public void updateAccount(String email, String phone) {
		sqlQuery = "UPDATE bookyourtrek.user SET phone = ? WHERE email = ?;";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setString(1, phone);
			ps.setString(2, email);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
